import 'dart:async';

import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/user.dart';
import 'package:qgchat/http/models/UserAcitvtyModel.dart';

class UserActivtyBloc implements BlocBase {
  int _page = 0; //初始化第一页
  int limit = 10;

  List<UserAcitvtyModel> list = []; //初始化数据
  bool isFirstLoading = true; // 是否第一次加载

  UserActivtyBloc() {
    getFansList();
  }

  @override
  void dispose() {
    activtyController.close();
  }

// STREAMS======================================================================

  /// 动态stream
  StreamController<List<UserAcitvtyModel>> activtyController =
      StreamController.broadcast();
  Sink<List<UserAcitvtyModel>> get _fansSink => activtyController.sink;
  Stream<List<UserAcitvtyModel>> get outFansStream => activtyController.stream;

// HANDLING=====================================================================

  /// 获取动态用户列表
  void getFansList() async {
    _page = _page + 1;
    List<UserAcitvtyModel> result =
        await UserApis.getAcitvity(page: _page, limit: limit);
    if (result.isNotEmpty) {
      isFirstLoading = false;
      list.addAll(result);
      _fansSink.add(list);
    } else {
      if (!isFirstLoading) showToast("已经没有了喔~ww");
      isFirstLoading = false;
    }
  }

  // 刷新动态用户列表
  void onloadList() {
    _page = 0;
    list = [];
    getFansList();
  }
}
